<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="type == 'check' && screenWidth > 1750 ? 17 : 24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span
              >估价业务 - <span v-if="pageType == 'add'">添加房源</span
              ><span v-if="pageType == 'edit'">修改房源</span
              ><span v-if="pageType == 'check'">审核房源</span></span
            >
            <el-link
              style="
                float: right;
                padding: 3px 0;
                font-size: 16px;
                font-weight: bold;
              "
              type="primary"
              v-if="houseInfo.EID"
              >业务编号：{{ houseInfo.EID }}</el-link
            >
          </div>
          <el-form :inline="true" label-width="120px">
            <el-form-item label="经办人:">
              <span>{{
                houseInfo.HInputStaff ? houseInfo.HInputStaff : userName
              }}</span>
            </el-form-item>
            <el-form-item label="连锁店:">
              <span>{{ houseInfo.HMultipleShopName }}</span>
            </el-form-item>
            <el-form-item label="业务编号:" v-if="pageType !== 'add'">
              <span>{{ houseInfo.EID }}</span>
            </el-form-item>
            <el-form-item label="资料查询:">
              <el-link
                type="primary"
                class="mr10"
                @click="handleTurn('风貌建筑查看')"
                >风貌建筑查看</el-link
              >
              <el-link type="primary" class="mr10" @click="handleTurn('转件')"
                >转件</el-link
              >
              <el-link
                type="primary"
                class="mr10"
                @click="handleTurn('对接数据')"
                >对接数据</el-link
              >
              <el-link
                type="primary"
                class="mr10"
                @click="handleTurn('不可贷款房屋')"
                >不可贷款房屋</el-link
              >
              <el-link
                type="primary"
                class="mr10"
                @click="handleTurn('图片审核')"
                v-if="type == 'check'"
                >图片审核</el-link
              >
              <el-link
                type="primary"
                class="mr10"
                @click="getRoomsInfo"
                v-if="type == 'check'"
                >房间资料</el-link
              >
            </el-form-item>
            <el-form-item
              label="验资结果:"
              v-if="type == 'check' && houseInfo.HPropertyRight == 5"
            >
              {{
                houseInfo.yz !== null
                  ? houseInfo.yz + ";请再次确认"
                  : "无验资记录，请再次确认！"
              }}
            </el-form-item>
          </el-form>
          <el-divider content-position="left"
            ><i class="el-icon-edit"></i
          ></el-divider>
          <el-form
            :model="houseInfo"
            :rules="rules"
            ref="houseInfoForm"
            class="demo-form-inline mt20"
            label-width="140px"
            element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.8)"
            v-loading="loading"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="房屋区域：" prop="district">
                  <el-cascader
                    v-model="houseInfo.district"
                    size="small"
                    :options="district_options"
                    @change="districtChange"
                    filterable
                    clearable
                    ref="district"
                  ></el-cascader>
                </el-form-item>
              </el-col>

              <el-col :span="16">
                <el-form-item label="门牌：" prop="HDoorplate">
                  <el-input
                    v-model="houseInfo.HDoorplate"
                    size="small"
                    placeholder="请输入门牌"
                    clearable
                    ref="HDoorplate"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="产别：" prop="HPropertyRight">
                  <el-select
                    v-model="houseInfo.HPropertyRight"
                    size="small"
                    filterable
                    placeholder="请选择"
                    ref="HPropertyRight"
                    clearable
                    @change="handleChangePropertyRight"
                  >
                    <el-option
                      v-for="item in dict_property_right"
                      :key="item.PID"
                      :label="item.PName"
                      :value="item.PID"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item label="产权管理单位：" prop="HRightCompany">
                  <el-row type="flex" v-if="showEditRightCompany">
                    <template
                      v-if="
                        houseInfo.HPropertyRight === '5' ||
                        houseInfo.HPropertyRight === '11'
                      "
                    >
                      <el-cascader
                        v-model="houseInfo.rightCompany"
                        size="small"
                        :options="dict_house_admin_gong_options"
                        clearable
                        style="width: 380px"
                        ref="HRightCompany"
                        @change="HRightCompanyChange(1)"
                      ></el-cascader>
                    </template>
                    <template v-else-if="houseInfo.HPropertyRight === '6'">
                      <el-select
                        v-model="houseInfo.HRightCompanyValue"
                        size="small"
                        filterable
                        placeholder="请选择"
                        ref="HRightCompany"
                        @change="HRightCompanyChange(2)"
                        clearable
                      >
                        <el-option
                          v-for="item in dict_house_admin_qi"
                          :key="item.AID"
                          :label="item.AName"
                          :value="item.AName"
                        >
                        </el-option>
                      </el-select>
                      <!-- 企产非合作 -->
                      <el-input
                        v-model="houseInfo.HRightCompanyValueInput"
                        size="small"
                        placeholder="请输入"
                        class="ml10"
                        clearable
                        ref="HRightCompany"
                        :disabled="hasRightCompany"
                        @input="HRightCompanyChange(3)"
                      ></el-input>
                    </template>
                    <template v-else>
                      <!-- 协议 -->
                      <el-input
                        v-model="houseInfo.HRightCompany"
                        size="small"
                        placeholder="请输入"
                        class="ml10"
                        clearable
                        ref="HRightCompany"
                        @input="HRightCompanyChange(4)"
                      ></el-input>
                    </template>
                  </el-row>
                  <template v-else>
                    <el-input
                      v-model="houseInfo.HRightCompany"
                      size="small"
                      placeholder="请输入"
                      class="ml10"
                      clearable
                      ref="HRightCompany"
                    ></el-input>
                  </template>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="房本真实产别：" prop="HPropertyRightReal">
                  <el-select
                    v-model="houseInfo.HPropertyRightReal"
                    size="small"
                    filterable
                    placeholder="请选择"
                    ref="HPropertyRight"
                    clearable
                    @change="handleChangePropertyRight"
                  >
                    <el-option
                      v-for="item in dict_property_right_real_Options"
                      :key="item.PID"
                      :label="item.PName"
                      :value="item.PID"
                      :disabled="item.Visible == 0"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col class="mb20">
                <el-divider></el-divider>
              </el-col>

              <el-col :span="8">
                <el-form-item label="房本编号：" prop="Hfb">
                  <el-input
                    v-model="houseInfo.Hfb"
                    size="small"
                    placeholder="请输入房本编号"
                    min="1"
                    max="50"
                    clearable
                    ref="Hfb"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="企产信价格："
                  prop="qichanxin"
                  v-if="houseInfo.HPropertyRight == 6"
                >
                  <el-input
                    v-model="houseInfo.qichanxin"
                    size="small"
                    placeholder="请输入企产信价格"
                    type="text"
                    clearable
                    ref="qichanxin"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="租赁合同号：" prop="HContract">
                  <el-input
                    v-model="houseInfo.HContract"
                    size="small"
                    placeholder="租赁合同号"
                    max="50"
                    min="1"
                    clearable
                    ref="HContract"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="居室间数：" prop="HRoomCount">
                  <el-input
                    v-model="houseInfo.HRoomCount"
                    size="small"
                    placeholder="请输入居室间数"
                    type="text"
                    clearable
                    ref="HRoomCount"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="居室间号：" prop="HHouseNo">
                  <el-input
                    v-model="houseInfo.HHouseNo"
                    size="small"
                    placeholder="请输入居室间号"
                    clearable
                    ref="HHouseNo"
                  ></el-input>
                </el-form-item>
              </el-col>
              <!-- <el-col :span="8">
                <el-form-item label="计租面积：" prop="HRentSquare">
                  <el-input
                    v-model="houseInfo.HRentSquare"
                    size="small"
                    placeholder="0"
                    type="text"
                    disabled
                    ref="HRentSquare"
                  ></el-input>
                </el-form-item>
              </el-col> -->
              <el-col :span="8" style="margin-bottom: 1px">
                <el-form-item label="房型：" prop="HouseStyle">
                  <el-cascader
                    v-model="houseInfo.styleValue"
                    size="small"
                    :options="style_options"
                    @change="styleChange"
                    filterable
                    clearable
                    ref="styleValue"
                  ></el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="独用面积：" prop="HSeperateSquare">
                  <el-input
                    v-model="houseInfo.HSeperateSquare"
                    size="small"
                    placeholder="请输入独用面积"
                    type="text"
                    clearable
                    ref="HSeperateSquare"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="伙用面积：" prop="HShareSquare">
                  <el-input
                    :disabled="HShareSquareDisabled"
                    v-model="houseInfo.HShareSquare"
                    size="small"
                    placeholder="请输入伙用面积"
                    type="text"
                    clearable
                    ref="HShareSquare"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="伙用户数：" prop="HShareNumber">
                  <el-input
                    :disabled="HShareNumberDisabled"
                    v-model="houseInfo.HShareNumber"
                    size="small"
                    placeholder="请输入伙用户数"
                    type="text"
                    clearable
                    ref="HShareNumber"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col class="mb20">
                <el-divider></el-divider>
              </el-col>
              <el-col :span="8">
                <el-form-item label="原承租人：" prop="HFormerRenter">
                  <el-input
                    v-model="houseInfo.HFormerRenter"
                    size="small"
                    placeholder="请输入原承租人"
                    clearable
                    ref="HFormerRenter"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系电话：" prop="HFormerTelephone">
                  <el-input
                    v-model="houseInfo.HFormerTelephone"
                    size="small"
                    placeholder="请输入联系电话"
                    clearable
                    ref="HFormerTelephone"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="工作单位：" prop="HFormerWorkPlace">
                  <el-input
                    v-model="houseInfo.HFormerWorkPlace"
                    size="small"
                    placeholder="请输入工作单位"
                    min="1"
                    max="200"
                    clearable
                    ref="HFormerWorkPlace"
                  ></el-input>
                </el-form-item>
              </el-col>
              <template>
                <el-col :span="houseInfo.HPropertyRight != '5' ? 16 : 24">
                  <el-form-item label="身份证号：" prop="HFormerIDCard">
                    <yanzi-request
                      :idCard="houseInfo.HFormerIDCard"
                      :eid="houseInfo.EID"
                      :yzMsg="houseInfo.yzMsg"
                      :disabled="houseInfo.HPropertyRight != '5'"
                      @yanziSuccess="yanziSuccess"
                      @changeIDCard="changeIDCard"
                    />
                  </el-form-item>
                </el-col>
              </template>
              <el-col :span="8">
                <el-form-item label="置换类型：" prop="HMetaType">
                  <el-select
                    v-model="houseInfo.HMetaType"
                    size="small"
                    filterable
                    placeholder="请选择"
                    clearable
                    ref="HMetaType"
                  >
                    <el-option
                      v-for="item in dict_meta_type"
                      :key="item.MID"
                      :label="item.Mname"
                      :value="item.MID"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <!-- <el-col :span="8">
                <el-form-item label="原因：" prop="reason">
                  <el-select
                    v-model="houseInfo.reason"
                    size="small"
                    filterable
                    placeholder="请选择"
                    clearable
                    ref="reason"
                  >
                    <el-option
                      v-for="item in reason_type"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col> -->
              <el-col :span="8">
                <el-form-item label="成交中介：" prop="Cjzj">
                  <el-select
                    v-model="houseInfo.Cjzj"
                    size="small"
                    filterable
                    placeholder="请选择"
                    clearable
                    ref="Cjzj"
                  >
                    <el-option
                      v-for="item in dict_cjzj"
                      :key="item.CID"
                      :label="item.CName"
                      :value="item.CName"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <!-- <el-col :span="8">
                <el-form-item
                  label="符合置换条件："
                  prop="iszhihuan"
                  v-if="pageType == 'check'"
                >
                  <el-switch
                    v-model="houseInfo.iszhihuan"
                    active-color="#13ce66"
                    inactive-color="#ccc"
                    active-value="1"
                    inactive-value="0"
                  >
                  </el-switch>
                </el-form-item>
              </el-col> -->
              <el-col :span="8">
                <el-form-item label="房屋管理形式：" prop="zhwflag">
                  <el-select
                    v-model="houseInfo.zhwflag"
                    size="small"
                    filterable
                    placeholder="请选择"
                    clearable
                    ref="zhwflag"
                  >
                    <el-option
                      v-for="item in houseXingshi"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="有无公证：" prop="gongzheng">
                  <el-select
                    v-model="houseInfo.gongzheng"
                    size="small"
                    placeholder="请选择"
                    clearable
                    ref="gongzheng"
                  >
                    <el-option label="请选择" value="-"> </el-option>
                    <el-option label="无公证" value="无公证"> </el-option>
                    <el-option label="有公证" value="有公证"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="孩子是否今年上学：" prop="isshangxue">
                  <el-select
                    v-model="houseInfo.isshangxue"
                    size="small"
                    filterable
                    placeholder="请选择"
                    clearable
                    ref="isshangxue"
                  >
                    <el-option label="是" value="是"> </el-option>
                    <el-option label="否" value="否"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="视频：" v-if="type == 'check'">
                  <span>{{ houseInfo.hasvedio }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="学片：" prop="xuepian">
                  <el-input
                    v-model="houseInfo.xuepian"
                    size="small"
                    placeholder="请输入"
                    clearable
                    ref="xuepian"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item
                  label="驳回原因："
                  prop="backReason"
                  v-if="type == 'check'"
                >
                  <el-row type="flex" justify="between">
                    <el-input
                      v-model="houseInfo.backReason"
                      placeholder="请输入"
                      type="textarea"
                      value="123"
                      clearable
                      ref="backReason"
                    ></el-input>
                    <el-button type="info" class="ml10" @click="bohuiHandle"
                      >记错驳回</el-button
                    >
                  </el-row>
                </el-form-item>
                <el-form-item label="备注：" prop="HRemark">
                  <el-input
                    v-model="houseInfo.HRemark"
                    placeholder="请输入"
                    type="textarea"
                    clearable
                    ref="HRemark"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-row type="flex" justify="end">
                  <el-form-item>
                    <el-button
                      type="primary"
                      @click="submitHouseInfo"
                      v-if="!isShowLoadingSub"
                      >提 交</el-button
                    >
                    <el-button type="primary" :loading="isShowLoadingSub" v-else
                      >提交中</el-button
                    >
                    <el-button
                      type="info"
                      @click="resetForm"
                      v-if="pageType == 'add'"
                      >重 置</el-button
                    >
                    <el-button
                      type="info"
                      @click="backPage"
                      v-if="pageType != 'add'"
                      >返回列表页</el-button
                    >
                  </el-form-item>
                  <el-form-item v-if="type == 'check'">
                    <template v-if="!isCheckSuccess">
                      <el-button
                        type="primary"
                        @click="checkedHandle"
                        v-if="!isShowLoading"
                        >审核通过</el-button
                      >
                      <el-button type="primary" :loading="isShowLoading" v-else
                        >审核中</el-button
                      >
                    </template>
                    <el-popconfirm
                      title="确定要发送置换单么？"
                      @confirm="handleSend"
                    >
                      <el-button
                        type="warning"
                        slot="reference"
                        class="ml10"
                        :disabled="!isCheckSuccess"
                        v-if="houseInfo.HPropertyRight != '6'"
                        v-hasPermi="['jgqd:baseInfo:sendMessage']"
                        >发送置换单
                      </el-button>
                    </el-popconfirm>
                  </el-form-item>
                </el-row>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="screenWidth > 1750 ? 7 : 24" v-if="type == 'check'">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>操作历史</span>
          </div>
          <el-table :data="list" stripe border style="width: 100%">
            <el-table-column
              prop="BDATE"
              label="日期"
              width="100"
              :show-overflow-tooltip="true"
            >
            </el-table-column>
            <el-table-column prop="BSource" label="录入人" width="100">
            </el-table-column>
            <el-table-column
              prop="BInfo"
              label="详细"
              :show-overflow-tooltip="true"
            >
            </el-table-column>
            <el-table-column
              prop="btype"
              label="状态"
              :show-overflow-tooltip="true"
            >
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <RoomInformation
      @changeRoomInformationState="changeRoomInformationState"
      ref="room"
    />
  </div>
</template>
<script>
import {
  houseAdd,
  houseUpdate,
  getHouseUpdateDetail,
  junduList,
  checkPass,
  checkBack,
  houseCheckDetail,
} from "@/api/jgqd/baseInfo";
import { sendMetaBill } from "@/api/jgqd/replacementOrder";
import { deepClone } from "@/utils/mis";
import { mapGetters } from "vuex";
import { validNumber, validIntNumber } from "@/utils/validate";
import { OALink, PhpLink } from "@/utils/baseUrl";
import YanziRequest from "@/views/components/YanziRequest";
import RoomInformation from "@/views/components/RoomInformation";
const defaultHouseInfo = {
  HDistrict: null, //区域
  HRightCompany: null,
  HRemark: "无",
  HHouseNo: undefined,
  HRoomCount: undefined,
  HFormerWorkPlace: undefined,
  HFormerTelephone: undefined,
  HFormerIDCard: undefined,
  HFormerRenter: undefined,
  HSellPrice: undefined,
  HShareNumber: undefined,
  HShareSquare: undefined,
  HSeperateSquare: undefined,
  HRentSquare: undefined,
  HDoorplate: undefined,
  HContract: undefined,
  HAddress: null,
  HID: "",
  Hfb: "",
  Cjzj: "",
  qichanxin: "",
  HouseStyle: undefined,
  zhwflag: "",
  isshangxue: "",
  gongzheng: undefined,
  xuepian: "",
  HPropertyRight: "",
  HBusinessType: "05",
};
export default {
  name: "HouseInput",
  components: { YanziRequest, RoomInformation },
  props: ["HID", "type"],
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg1 = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/;
        // const reg = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/;
        if (reg1.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    var IDCardCheck = (rule, value, callback) => {
      if (value) {
        const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的身份证号"));
        }
      } else {
        callback(new Error("身份证号不能为空"));
      }
    };
    var validNumberCheck = (rule, value, callback) => {
      // 价格值
      if (value) {
        if (!validNumber(value)) {
          callback(new Error("必须为数字值,可以保留两位小数"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    // var validIntNumberCheck = (rule, value, callback) => {
    //   // 整数值
    //   if (value) {
    //     if (!validIntNumber(value)) {
    //       callback(new Error("必须为正整数值"));
    //     } else {
    //       callback();
    //     }
    //   } else {
    //     callback();
    //   }
    // };
    //居室间数正则
    var HRoomCountValid = (rule, value, callback) => {
      if (value) {
        if (!validIntNumber(value)) {
          callback(new Error("必须为正整数值"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入居室间数"));
      }
    };
    //独用面积正则
    var HSeperateSquareValid = (rule, value, callback) => {
      if (value) {
        if (!validNumber(value)) {
          callback(new Error("必须为数字值,可以保留两位小数"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入独用面积"));
      }
    };
    // 伙用面积
    var HShareSquareValid = (rule, value, callback) => {
      if (value) {
        if (!validNumber(value)) {
          callback(new Error("必须为数字值,可以保留两位小数"));
        } else {
          callback();
        }
      } else {
        if (typeof this.houseInfo.styleValue != "undefined") {
          if (this.houseInfo.styleValue[0] == 5) {
            callback(new Error("请输入伙用面积"));
          } else {
            callback();
          }
        }
      }
    };
    // 伙用户数
    var HShareNumberValid = (rule, value, callback) => {
      if (value) {
        if (!validIntNumber(value)) {
          callback(new Error("必须为正整数值"));
        } else {
          callback();
        }
      } else {
        if (typeof this.houseInfo.styleValue != "undefined") {
          if (this.houseInfo.styleValue[0] == 5) {
            callback(new Error("请输入伙用户数"));
          } else {
            callback();
          }
        }
      }
    };
    return {
      HShareSquareDisabled: false,
      HShareNumberDisabled: false,
      OALink: OALink,
      EID: "",
      pageType: "add",
      isShowLoading: false,
      loading: false,
      isShowLoadingSub: false,
      houseInfo: Object.assign({}, defaultHouseInfo),
      districtOptions: [],
      showEditRightCompany: true,
      hasRightCompany: false,
      isCheckSuccess: false, //是否审核成功
      rules: {
        qichanxin: [{ validator: validNumberCheck, trigger: "blur" }],
        HRoomCount: [
          { required: true, validator: HRoomCountValid, trigger: "blur" },
        ],
        HContract: [
          { required: false, message: "请输入租赁合同号", trigger: "blur" },
        ],
        HHouseNo: [
          { required: false, message: "请输入居室间号", trigger: "blur" },
        ],
        HShareNumber: [
          { required: false, validator: HShareNumberValid, trigger: "blur" },
        ],
        HSeperateSquare: [
          { required: true, validator: HSeperateSquareValid, trigger: "blur" },
        ],
        HShareSquare: [
          { required: false, validator: HShareSquareValid, trigger: "blur" },
        ],
        HDoorplate: [
          { required: true, message: "请输入门牌号", trigger: "blur" },
        ],
        Hfb: [{ required: true, message: "请输入房本编号", trigger: "blur" }],
        HMetaType: [
          { required: true, message: "请选择置换类型", trigger: "change" },
        ],
        // backReason: [
        //   { required: true, message: "请输入驳回原因", trigger: "blur" },
        // ],
        HFormerRenter: [
          { required: true, message: "请输入原承租人", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur",
          },
        ],
        HFormerTelephone: [
          { required: true, validator: checkPhone, trigger: "blur" },
        ],
        HFormerIDCard: [
          { required: true, validator: IDCardCheck, trigger: "blur" },
        ],
        district: [
          { required: true, message: "请选择房屋区域", trigger: "change" },
        ],
        HRightCompany: [
          { required: true, message: "产权管理单位必填", trigger: "change" },
        ],
        HPropertyRight: [
          { required: true, message: "请选择产别", trigger: "change" },
        ],
        HPropertyRightReal: [
          { required: true, message: "请选择房本真实产别", trigger: "change" },
        ],
        HouseStyle: [
          { required: true, message: "请选择房型", trigger: "change" },
        ],
        Cjzj: [
          { required: true, message: "请选择成交中介", trigger: "change" },
        ],
        gongzheng: [
          { required: true, message: "请选择公证类型", trigger: "change" },
        ],
        zhwflag: [
          { required: true, message: "请选择房屋管理形式", trigger: "change" },
        ],
      },
      list: [],
      reason_type: [
        { value: "1", label: "经核查为不可售" },
        { value: "2", label: "亲属间置换" },
        { value: "3", label: "津房置换回购房屋" },
        { value: "4", label: "企产置换业务" },
        { value: "9", label: "夫妻间置换（满两年）" },
      ],
      houseXingshi: [
        { value: "0", label: "正常" },
        { value: "1", label: "新纳" },
        { value: "2", label: "托管" },
      ],
      screenWidth: document.body.clientWidth,
      dict_property_right_real_Options: [],
    };
  },
  computed: {
    ...mapGetters([
      "userName",
      "userId",
      "shopName",
      "shopID",
      "dict_property_right",
      "dict_right_company",
      "district_options",
      "style_options",
      "dict_cjzj",
      "dict_house_admin_qi",
      "dict_house_admin_gong_options",
      "dict_meta_type",
      "dict_property_right_real",
      "uuid",
      "userName",
    ]),
    // checkYanzi() {
    //   //保定道店不用验资
    //   let isYanzi = false;
    //   if (this.pageType == "add") {
    //     isYanzi = this.houseInfo.HPropertyRight != "5";
    //   } else {
    //     // edit/check
    //     isYanzi =
    //       this.houseInfo.HPropertyRight != "5" ||
    //       this.houseInfo.HMultipleShop == "30";
    //   }

    //   return isYanzi;
    // },
    // disabled() {
    //   if (this.pageType == "add" || !this.houseInfo.yz) {
    //     return false;
    //   } else {
    //     return true;
    //   }
    // },
  },
  watch: {
    screenWidth(val) {
      this.screenWidth = val;
    },
    "houseInfo.HFormerIDCard": {
      handler() {
        if (this.pageType == "add" && this.shopID == "30") {
          this.rules.HFormerIDCard[0].required = false;
        } else if (this.houseInfo.HMultipleShop == "30") {
          this.rules.HFormerIDCard[0].required = false;
        }
      },
      immediate: true,
    },
  },
  created() {
    if (this.HID) {
      if (this.type) {
        this.pageType = "check";
        this.getLogs();
      } else {
        this.pageType = "edit";
      }
      this.dict_property_right_real_Options = this.dict_property_right_real;
      this.getHouseUpdateDetail();
    } else {
      this.pageType = "add";
      this.houseInfo.HMultipleShopName = this.shopName;
      this.dict_property_right_real_Options =
        this.dict_property_right_real.filter((el) => {
          return el.Visible == 1;
        });
    }
  },
  mounted() {
    const that = this;
    console.log(`this.shopID`, this.shopID);
    window.onresize = () => {
      return (() => {
        window.screenWidth = document.body.clientWidth;
        that.screenWidth = window.screenWidth;
      })();
    };
  },
  methods: {
    //房间资料
    getRoomsInfo() {
      this.$nextTick(() => {
        this.$refs["room"].openDialog(this.HID);
      });
    },
    //房间资料关闭
    changeRoomInformationState() {
      this.disabled = false;
    },
    handleTurn(type) {
      let url = "";
      if (type === "风貌建筑查看") {
        this.$router.push("/JgqdBaseInfo/SeeBuilding");
        return;
      } else if (type === "转件") {
        url = OALink + "zhiye/newcj/mis/MetaEms/MetaEmsSend.asp";
      } else if (type === "对接数据") {
        let hno = "";
        //为null变成""
        if (!this.houseInfo.HContract) {
          hno = "";
          // 否则看长度
        } else {
          if (
            this.houseInfo.HContract.length == 17 ||
            this.houseInfo.HContract.length == 19
          ) {
            hno = this.houseInfo.HContract;
          } else {
            hno = "";
          }
        }
        let url =
          PhpLink +
          "api/keji.dui_jie/index?hno=" +
          hno +
          "&uname=" +
          this.userName +
          "&uuid=" +
          this.uuid;
        window.open(url, "_blank");
      } else if (type === "不可贷款房屋") {
        this.$router.push("/JgqdBaseInfo/BanLoanHouse");
        return;
      } else if (type === "图片审核") {
        url = OALink + "zhiye/newcj/meta/showimagelist.asp?hid=" + this.HID;
      }
      window.open(url, "     ");
    },
    getLogs() {
      junduList({ HID: this.HID })
        .then((res) => {
          if (res.success) {
            this.list = res.data.data;
          }
        })
        .catch(() => {
          this.msgError("error");
        });
    },
    backPage() {
      this.$router.go(-1);
    },
    changeIDCard(val) {
      this.houseInfo.HFormerIDCard = val; //身份证号
    },
    handleSend() {
      // 发送置换单
      sendMetaBill({ HID: this.HID })
        .then((res) => {
          if (res.success) {
            this.msgSuccess("成功发送置换单");
            this.$router.push({ name: "HouseList" });
          } else {
            this.msgError(res.msg);
          }
        })
        .catch(() => {});
    },
    //审核通过
    checkedHandle() {
      this.houseInfo.HID = this.HID;
      checkPass(this.houseInfo)
        .then((res) => {
          if (res.success) {
            this.msgSuccess("审核通过！");
            this.isCheckSuccess = true;
            this.getLogs();
          } else {
            this.msgFormsWarning(res.data);
          }
        })
        .catch((err) => {
          this.msgError(err);
        });
    },
    //审核驳回
    bohuiHandle() {
      if (!this.houseInfo.backReason) {
        this.msgError("驳回原因必填！");
        return;
      }
      let data = {
        HID: this.HID,
        reason: this.houseInfo.backReason,
      };
      checkBack(data)
        .then((res) => {
          if (res.success) {
            this.$router.push({ name: "HouseList" });
            this.msgSuccess("驳回成功！");
          } else {
            this.msgFormsWarning(res.data);
          }
        })
        .catch((err) => {
          this.msgError(err);
        });
    },
    yanziSuccess(data) {
      this.houseInfo.yzMsg = data.res;
      this.houseInfo.HFormerIDCard = data.idCard;
      this.houseInfo.yzno = data.yzno;
    },
    getHouseUpdateDetail() {
      getHouseUpdateDetail({ HID: this.HID })
        .then((res) => {
          if (res.success) {
            let result = res.data;
            result.gongzheng = res.data.gongzhe;
            result.Hfb = res.data.hfb;
            result.Cjzj = res.data.ECjzj;
            result.HouseStyle = res.data.EHouseType;
            result.HMetaType = res.data.hmetatype;
            result.HouseStyleDetail = res.data.EHouseTypeDetail;
            result.isshangxue = res.data.jinNianShangxue;
            result.district = [result.HDistrict, result.HAddress];
            result.styleValue = [result.EHouseType, result.EHouseTypeDetail];
            // 一开始进来处理伙用面积和伙用户数
            if (result.styleValue[0] == 5) {
              this.rules.HShareSquare[0].required = true;
              this.rules.HShareNumber[0].required = true;
              this.HShareSquareDisabled = false;
              this.HShareNumberDisabled = false;
            } else {
              this.rules.HShareSquare[0].required = false;
              this.rules.HShareNumber[0].required = false;
              this.HShareSquareDisabled = true;
              this.HShareNumberDisabled = true;
            }
            if (result.HDistrict == "0") {
              result.HDistrict = null;
            }
            if (result.HAddress == "0") {
              result.HAddress = null;
            }
            // if (result.HRightCompany) {
            // this.showEditRightCompany = false;
            // }

            this.houseInfo = deepClone(result);
            //公产/区管产
            if (
              this.houseInfo.HPropertyRight === "5" ||
              this.houseInfo.HPropertyRight === "11"
            ) {
              // 处理产权管理单位区域不对应
              this.dict_house_admin_gong_options.map((ele) => {
                ele.children.map((item) => {
                  if (item.AName === this.houseInfo.HRightCompany) {
                    this.houseInfo.HRightCompanyHDistrict = ele.DID;
                  }
                });
              });
              this.houseInfo.rightCompany = [
                this.houseInfo.HRightCompanyHDistrict,
                this.houseInfo.HRightCompany.trim(),
              ];
              this.rules.HContract[0].required = true;
              this.rules.HHouseNo[0].required = true;
            }
            // 企产
            else if (this.houseInfo.HPropertyRight == 6) {
              //看下拉列表里有没有,没有就是自己输入的
              let index = this.dict_house_admin_qi.findIndex((item) => {
                return item.AName == this.houseInfo.HRightCompany;
              });
              // 没找到是自己输入的
              if (index == -1) {
                this.houseInfo.HRightCompanyValueInput =
                  this.houseInfo.HRightCompany;
                this.houseInfo.HRightCompanyValue = "请选择";
              }
              //找到就是赋值下拉选
              else {
                this.houseInfo.HRightCompanyValue =
                  this.houseInfo.HRightCompany;
              }
            }
            // if (this.pageType === "edit" || this.pageType === "check") {
            //   this.houseInfo.HRightCompanyValue = this.houseInfo.HRightCompany;
            //   this.HRightCompanyChange("2");
            // }
          }
        })
        .catch((err) => {
          this.msgError(err);
        });
    },
    submitHouseInfo() {
      this.$refs["houseInfoForm"].validate((valid) => {
        if (valid) {
          if (this.pageType === "add") {
            this.isShowLoading = true;
            let data = this.houseInfo;
            data.HMultipleShop = this.shopID;
            data.HInputStaff = this.userId;
            houseAdd(data)
              .then((res) => {
                this.isShowLoading = false;
                if (res.success) {
                  this.msgSuccess("添加房源成功");
                  this.resetForm();
                  this.$router.push({ name: "HouseList" });
                } else {
                  this.msgFormsWarning(res.data);
                }
              })
              .catch(() => {
                this.isShowLoading = false;
              });
          } else if (this.pageType === "check") {
            this.isShowLoadingSub = true;
            this.houseInfo.HID = this.HID;
            this.houseInfo.EHouseType = this.houseInfo.HouseStyle;
            this.houseInfo.EHouseTypeDetail = this.houseInfo.HouseStyleDetail;
            this.houseInfo.ECjzj = this.houseInfo.Cjzj;
            // if (this.houseInfo.district.length>0) {
            //   this.houseInfo.HDistrict = this.houseInfo.district[0]
            // }

            houseCheckDetail(this.houseInfo)
              .then((res) => {
                this.isShowLoadingSub = false;
                if (res.success) {
                  this.msgSuccess("提交信息成功！");
                } else {
                  this.msgFormsWarning(res.data);
                }
              })
              .catch(() => {
                this.isShowLoadingSub = false;
              });
          } else {
            this.houseInfo.HID = this.HID;
            // this.houseInfo.HMultipleShop = "18"; //连锁店字段获取过来字段为0
            houseUpdate(this.houseInfo)
              .then((res) => {
                this.isShowLoading = false;
                if (res.success) {
                  this.msgSuccess("修改房源成功");
                  this.$store.dispatch("delVisitedViews", this.$route);
                  this.$router.push({ name: "HouseList" });
                } else {
                  this.msgFormsWarning(res.data);
                }
              })
              .catch(() => {
                this.isShowLoading = false;
              });
          }
        }
      });
    },
    resetForm() {
      this.$refs["houseInfoForm"].resetFields();
      this.houseInfo = deepClone(Object.assign({}, defaultHouseInfo));
    },
    districtChange(e) {
      if (e && e.length >= 1) {
        this.houseInfo.HDistrict = e[0];
        this.houseInfo.HAddress = e[1];
      }
    },
    styleChange(e) {
      if (e && e.length >= 1) {
        if (e[0] == 5) {
          this.rules.HShareSquare[0].required = true;
          this.rules.HShareNumber[0].required = true;
          this.HShareSquareDisabled = false;
          this.HShareNumberDisabled = false;
        } else {
          this.houseInfo.HShareSquare = "";
          this.houseInfo.HShareNumber = "";
          this.rules.HShareSquare[0].required = false;
          this.rules.HShareNumber[0].required = false;
          this.$refs["houseInfoForm"].clearValidate(["HShareSquare"]);
          this.$refs["houseInfoForm"].clearValidate(["HShareNumber"]);
          this.HShareSquareDisabled = true;
          this.HShareNumberDisabled = true;
        }
        this.houseInfo.HouseStyle = e[0];
        this.houseInfo.HouseStyleDetail = e[1];
      }
    },
    HRightCompanyChange(type) {
      this.$forceUpdate();
      if (type == "1") {
        if (this.houseInfo.rightCompany && this.houseInfo.rightCompany.length) {
          this.houseInfo.HRightCompany = this.houseInfo.rightCompany[1];
        }
      } else if (type == "2") {
        if (
          this.houseInfo.HRightCompanyValue &&
          this.houseInfo.HRightCompanyValue != "请选择"
        ) {
          this.hasRightCompany = true;
          this.houseInfo.HRightCompany = this.houseInfo.HRightCompanyValue;
        } else {
          this.hasRightCompany = false;
        }
      } else if (type == "3") {
        this.houseInfo.HRightCompany = this.houseInfo.HRightCompanyValueInput;
      }
    },
    handleChangePropertyRight(e) {
      if (this.pageType == "edit") {
        this.showEditRightCompany = true;
      }
      if (e == "5" || e == "11") {
        // 公产或者区管产，租赁合同号和居室间号为必填
        this.rules.HContract[0].required = true;
        this.rules.HHouseNo[0].required = true;
      } else {
        this.rules.HContract[0].required = false;
        this.rules.HHouseNo[0].required = false;
      }
    },
  },
};
</script>
